package org.shock.ss.sms.action.system;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.net.URLEncoder;

import org.shock.ss.sms.bean.FileBean;

import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.commons.utils.FileUtils;

import org.shock.ss.sms.action.BaseAction;

public class UpdateServerAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String drivers;
	private String currentPath;
	private StringBuffer navigation = new StringBuffer();
	private List list = new ArrayList();
	private String path = getRequest().getRealPath("/");
	private Integer totalFolder = 0;
	private Integer totalField = 0;

	@RightAnno(rightValue = "serverAdmin", rightNote = "查看文件")
	public String viewer() throws Exception{
		
		return SUCCESS;
	}

	@RightAnno(rightValue = "serverAdmin", rightNote = "系统更新")
	public String serverAdmin() throws Exception {
		//System.out.println("=============" + path);
		drivers = FileUtils.pj_getDrivers();
		currentPath = FileUtils.pj_path2link(path);
		File objFile = new File(path);
		File files[] = objFile.listFiles();
		if (objFile.getParent() == null) {
			navigation.append("<a href='#' disabled>已是最顶层目录</a> |");
		} else {
			navigation.append("<a href='serverAdmin.do?path="
					+ URLEncoder.encode(objFile.getParent(), "utf8")
					+ "'>上一级目录</a>  |");
			navigation
					.append("<a href='#' onClick='history.go(-1);'>后退</a> | <a href='#' onClick='history.go(1);'>前进</a> | <a href='serverAdmin.do?path="
							+ path
							+ "'>刷新</a> | <a href='serverAdmin.do'>首页</a>");
		}
		if (null == files) {
			throw new RuntimeException("该文件夹为空");
		}
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			FileBean f = new FileBean(file.getName(), FileUtils
					.pj_convertFileSize(file.length()), new SimpleDateFormat(
					"yyyy-M-d H:mm").format(file.lastModified()), URLEncoder
					.encode(file.getAbsolutePath(), "utf8"), file.isFile() ? 1
					: 0);
			if (file.isDirectory()) {
				totalFolder++;
				list.add(0, f);
			} else {
				totalField++;
				list.add(f);
			}
		}
		return SUCCESS;
	}

	public String getDrivers() {
		return drivers;
	}

	public void setDrivers(String drivers) {
		this.drivers = drivers;
	}

	public String getCurrentPath() {
		return currentPath;
	}

	public void setCurrentPath(String currentPath) {
		this.currentPath = currentPath;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public StringBuffer getNavigation() {
		return navigation;
	}

	public void setNavigation(StringBuffer navigation) {
		this.navigation = navigation;
	}

	public Integer getTotalFolder() {
		return totalFolder;
	}

	public void setTotalFolder(Integer totalFolder) {
		this.totalFolder = totalFolder;
	}

	public Integer getTotalField() {
		return totalField;
	}

	public void setTotalField(Integer totalField) {
		this.totalField = totalField;
	}

}
